# Clean up
rm(list = ls())
Sys.setenv(LANG = "en")

# Set working directory: please set your own
#setwd("~/Dropbox/sanctions_replication/")

# Load necessary packages
library(tidyverse)
library(readstata13)
library(lubridate)

# Import dataset
data <- read.dta13("data/survey_clean.dta")

# Make the plot
figure1 <- data %>%
  select(pbeliefs_vox_num, pbeliefs_up_num, pbeliefs_psoe_num,
         pmoral_vox_num, pmoral_up_num, pmoral_psoe_num,
         pharm_vox_num, pharm_up_num, pharm_psoe_num,
         normexpect_vox_num, normexpect_up_num, normexpect_psoe_num,
         smoral_vox_num, smoral_up_num, smoral_psoe_num,
         sharm_vox_num, sharm_up_num, sharm_psoe_num)  %>%
  `colnames<-`(c("Vox_Socially_Appropriate", "Podemos_Socially_Appropriate", "PSOE_Socially_Appropriate",
                 "Vox_Morally_Appropriate", "Podemos_Morally_Appropriate", "PSOE_Morally_Appropriate",
                 "Vox_Harmful", "Podemos_Harmful", "PSOE_Harmful",
                 "Vox_Socially_Appropriate_Others", "Podemos_Socially_Appropriate_Others", "PSOE_Socially_Appropriate_Others",
                 "Vox_Morally_Appropriate_Others", "Podemos_Morally_Appropriate_Others", "PSOE_Morally_Appropriate_Others",
                 "Vox_Harmful_Others", "Podemos_Harmful_Others", "PSOE_Harmful_Others")) %>%
  # make into long format
  pivot_longer(cols = c(Vox_Socially_Appropriate:PSOE_Harmful_Others),
               names_to = "Group") %>%
  group_by(Group) %>%
  # generate party variable
  mutate(Party = ifelse(Group %in% c("Vox_Socially_Appropriate", "Vox_Morally_Appropriate", "Vox_Harmful", 
                                     "Vox_Socially_Appropriate_Others", "Vox_Morally_Appropriate_Others", "Vox_Harmful_Others"), "Vox",
                        ifelse(Group %in% c("Podemos_Socially_Appropriate", "Podemos_Morally_Appropriate", "Podemos_Harmful",
                                            "Podemos_Socially_Appropriate_Others", "Podemos_Morally_Appropriate_Others", "Podemos_Harmful_Others"), "Podemos", 
                               "PSOE"))) %>%
  # Generate outcome variable
  mutate(Outcome = ifelse(Group %in% c("Vox_Socially_Appropriate", "Podemos_Socially_Appropriate", "PSOE_Socially_Appropriate"), "Socially Appropriate (1-6 scale)",
                          ifelse(Group %in% c("Vox_Morally_Appropriate", "Podemos_Morally_Appropriate", "PSOE_Morally_Appropriate"), "Morally Appropriate (1-6 scale)",
                                 ifelse(Group %in% c("Vox_Harmful", "Podemos_Harmful", "PSOE_Harmful"), "Harmful (inverted) (1-4 scale)",
                                        ifelse(Group %in% c("Vox_Socially_Appropriate_Others", "Podemos_Socially_Appropriate_Others", "PSOE_Socially_Appropriate_Others"), "Socially Appropriate (others) (1-6 scale)",
                                               ifelse(Group %in% c("Vox_Morally_Appropriate_Others", "Podemos_Morally_Appropriate_Others", "PSOE_Morally_Appropriate_Others"), "Morally Appropriate (others) (1-6 scale)",
                                                      ifelse(Group %in% c("Vox_Harmful_Others", "Podemos_Harmful_Others", "PSOE_Harmful_Others"), "Harmful (inverted) (others) (1-4 scale)",
                                                             "Gossip"))))))) %>%
  # Generate variable for first and second order expectations
  mutate(Order = ifelse(Outcome %in% c("Socially Appropriate (1-6 scale)", "Morally Appropriate (1-6 scale)", "Harmful (inverted) (1-4 scale)"), "Own views",
                        ifelse(Outcome %in% c("Socially Appropriate (others) (1-6 scale)", "Morally Appropriate (others) (1-6 scale)", "Harmful (inverted) (others) (1-4 scale)"), "Perception of others' views")))  %>%
  mutate(ordering = ifelse(Outcome %in% c("Socially Appropriate (1-6 scale)", "Morally Appropriate (1-6 scale)", "Harmful (inverted) (1-4 scale)"), 1,
                           ifelse(Outcome %in% c("Socially Appropriate (others) (1-6 scale)", "Morally Appropriate (others) (1-6 scale)", "Harmful (inverted) (others) (1-4 scale)"), 2))) %>%
  mutate(Order = fct_reorder(Order, ordering)) %>%
  # Calculate means etc
  mutate(n = sum(!is.na(value))) %>%
  mutate(mean = mean(value, na.rm = T)) %>%
  mutate(sd = sd(value, na.rm = T)) %>%
  mutate(std_error = sd/sqrt(n)) %>%
  mutate(ci_upper = mean + 1.96 * std_error) %>%
  mutate(ci_lower = mean - 1.96 * std_error) %>%
  ungroup() %>%
  ggplot(aes(x = Party, y = value, color = Party)) +
  geom_point(alpha = 0.02, position = position_jitter()) +
  scale_color_manual(values = c("purple", "red", "green4")) +
  facet_wrap(Order~Outcome, ncol = 3, nrow = 2, scales = "free_y") +
  theme_bw() +
  geom_errorbar(aes(x = Party, ymin = ci_lower, ymax = ci_upper, width = 0.1)) + 
  geom_point(aes(x = Party, y = mean)) +
  theme(legend.position = "none") +
  labs(x = "", y = "") +
  scale_y_continuous(breaks = c(1, 2, 3, 4, 5, 6))
figure1

ggsave("figures/figure1.png", plot = figure1, width = 20, height = 25, units = "cm")